﻿/********************************************************
 * 类名： MenuBLL.cs
 * 功能： 菜单业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-11-27	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using PSS.DAL.SystemManage;
using PSS.Entity.SystemManage;
using PSS.Utility;

namespace PSS.BLL.SystemManage
{
    public class MenuBLL
    {
        private readonly MenuDAL _dal = new MenuDAL();

        public string GetMenuParentNode()
        {
            var list = _dal.GetMenuParentNode();
            var model = new MenuEntity { MenuName = "主菜单", MenuId = new Guid("00000000-0000-0000-0000-000000000000"), ParentMenuId = Guid.NewGuid() };
            list.Add(model);
            return JsonHelper.Encode(list);
        }

        public string GetMenuChildNode(MenuQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel.MenuId);
            var list = _dal.GetMenuChildNode(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetMenuInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetMenuInfo(id));
        }

        public bool AddMenu(string data)
        {
            var model = new MenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuId = Guid.NewGuid();
                model.MenuName = row["MenuName"].ToString();
                model.Url = row["Url"].ToString();
                model.ParentMenuId = new Guid(row["ParentMenuId"].ToString());
                model.Flag = 1;
                model.MenuIdx = Convert.ToInt32(row["MenuIdx"].ToString());
            }
            return _dal.AddMenu(model);
        }

        public bool ModifyMenu(string data)
        {
            var model = new MenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuId = new Guid(row["MenuId"].ToString());
                model.MenuName = row["MenuName"].ToString();
                model.Url = row["Url"].ToString();
                model.MenuIdx = Convert.ToInt32(row["MenuIdx"].ToString());
            }
            return _dal.ModifyMenu(model);
        }

        public bool Remove(string id)
        {
            return _dal.Remove(id);
        }

        public bool RemoveParentMenuNode(string id)
        {
            var result = false;
            if (_dal.Remove(id))
            {
                if (_dal.RemoveChildMenuNode(id))
                {
                    result = true;
                }
            }
            return result;
        }

        public bool ModifyParentMenuNode(string data)
        {
            var model = new MenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuId = new Guid(row["MenuId"].ToString());
                model.MenuName = row["MenuName"].ToString();
                model.MenuIdx = Convert.ToInt32(row["MenuIdx"].ToString());
            }
            return _dal.ModifyParentMenuNode(model);
        }

        public bool AddParentMenuNode(string data)
        {
            var model = new MenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuId = Guid.NewGuid();
                model.MenuName = row["MenuName"].ToString();
                if (model.MenuId != Guid.Empty)
                {
                    model.ParentMenuId = new Guid(row["ParentMenuId"].ToString());
                }
                model.Flag = 0;
                model.MenuIdx = Convert.ToInt32(row["MenuIdx"].ToString());
            }
            return _dal.AddParentMenuNode(model);
        }

        public string LoadMenuList(string loginCode)
        {
            return JsonHelper.Encode(_dal.LoadMenuList(loginCode));
        }

        public string GetMenuList()
        {
            return JsonHelper.Encode(_dal.GetMenuList());
        }
    }
}
